package com.ssyu.dome.action;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.ssyu.dome.common.StringUtil;
import com.ssyu.dome.common.utl.BaseAction;
import com.ssyu.dome.entity.UserEntity;
import com.ssyu.dome.service.UserService;

public class UserAction extends BaseAction {
	private UserEntity user;// 实体-复用
	/* 创建业务层的对象 */
	private UserService userService = new UserService();
	private String msg;// 错误提示语句

	/**
	 * 登录
	 * 
	 * @return
	 */
	public String login() {
		// 根据邮箱和密码判断用户是否存在
		UserEntity userEntity = userService.login(user);
		if (userEntity == null) {
			// 如果用户不存在跳转到登录页面
			msg = "账号和密码错误!";
			return "tologinUI";

		} else {
			// 判断是有效账号，，还是无效账号
			if (userEntity.getIsActive() == UserEntity.ACCOUNT_VALID) {
				HttpSession session = ServletActionContext.getRequest()
						.getSession();
				session.setAttribute("userEntity", userEntity);

				// 判断，，如果是求职者，判断姓名电话填写没有
				if (userEntity.getRoleType() == UserEntity.ROLE_TALENT) {
					String username = userEntity.getUsername();
					String phone = userEntity.getPhone();
					if (StringUtil.isBlank(username)
							|| StringUtil.isBlank(phone)) {
						return "toBindInfoUI";
					} else {
						// 注册了就跳转主页
						return "toIndexUI";
					}
				}// 判断，，如果是公司就跳转公司页面
				else if (userEntity.getRoleType() == UserEntity.ROLE_COMPANY) {
					return "toCompanyUI";
				} else {
					// 其他就是管理员
					return "toAdminUI";
				}
			} else {
				msg = "此账号未激活!请查看邮件激活账号!";
				return "tologinUI";
			}

		}
	}

	/**
	 * 注销login
	 * 
	 * @return
	 */
	public String logout() {
		ServletActionContext.getRequest().getSession().invalidate();
		return "tologinUI";
	}

	/**
	 * 注册
	 * 
	 * @return
	 */
	public String addAccount() {
		int row = userService.addAccount(user);
		if (row > 0) {
			System.out.println("注册成功!");
		} else {
			System.out.println("注册失败!");

		}
		return "addAccount";
	}

	/**
	 * 用户信息绑定
	 * 
	 * @return
	 */
	public String toBindInfoUI() {
		return "toBindInfoUI";
	}

	/**
	 * 信息绑定
	 * 
	 * @return
	 */
	public String bindInfo() {
		user.setId(getTalentId());
		userService.bindInfoService(user);
		getTalent().setUsername(user.getUsername());
		getTalent().setPhone(user.getPhone());

		return "toIndexUI";
	}

	public UserEntity getUser() {
		return user;
	}

	public void setUser(UserEntity user) {
		this.user = user;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

}
